package com.example.administrator.himalaya.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.entity.rating.TopBean;
import com.example.administrator.himalaya.entity.rating.TopBeans;
import com.rock.teachlibrary.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发现--榜单
 */
public class RatingAdapter extends BaseExpandableListAdapter {

    private List<TopBeans> data;
    private LayoutInflater inflater;

    public RatingAdapter(Context context, List<TopBeans> data) {
        if (data != null) {
            this.data = data;
        } else {
            this.data = new ArrayList<>();
        }

        inflater = LayoutInflater.from(context);
    }

    public void update(List<TopBeans> data) {
        if (data != null) {
            this.data.clear();
            this.data.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getGroupCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.get(groupPosition).getList() != null ? data.get(groupPosition).getList().size() : 0;
    }

    @Override
    public TopBeans getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public TopBean getChild(int groupPosition, int childPosition) {
        return getGroup(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.rating_group_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        TextView groupTitle = (TextView) holder.getView(R.id.top_list_group_name);
        groupTitle.setText(getGroup(groupPosition).getTitle());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.rating_child_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        TextView childTitle = (TextView) holder.getView(R.id.top_list_item_child_title);
        TextView title1 = (TextView) holder.getView(R.id.top_list_item_child_title1);
        TextView title2 = (TextView) holder.getView(R.id.top_list_item_child_title2);
        ImageView imageView = (ImageView) holder.getView(R.id.top_list_item_image);

        childTitle.setText(getChild(groupPosition, childPosition).getTitle());
        title1.setText("1 " + getChild(groupPosition, childPosition).getFirstKResults().get(0).getTitle());
        title2.setText("2 " + getChild(groupPosition, childPosition).getFirstKResults().get(1).getTitle());

        ImageLoader.display(imageView, getChild(groupPosition, childPosition).getCoverPath());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private static class ViewHolder {
        private View convertView;
        private Map<Integer, View> map;

        private ViewHolder(View convertView) {
            this.convertView = convertView;
            map = new HashMap<>();
        }

        public View getView(int resId) {
            View view = null;
            if (map.containsValue(resId)) {
                view = map.get(resId);
            } else {
                view = convertView.findViewById(resId);
                map.put(resId, view);
            }

            return view;
        }
    }
}
